Handle all AggregateExceptions when using Task.Whenall() async

62

Handle all AggregateExceptions when using Task.Whenall() async -

Task aggregationTask = null;
try{
  var task1 = DoSomethingAsync();
  var task2 = DoSomethingElseAsync();
  aggregationTask = Task.WhenAll(task1, task2);
  await aggregationTask;
}catch(Exception ex){  
  if(aggregationTask?.Exception?.InnerExceptions != null && aggregationTask.Exception.InnerExceptions.Any())
    foreach (var innerEx in aggregationTask.Exception.InnerExceptions){
      // do your magic here
    }
}

Comments

Submit
0 Comments